<?php
namespace app\admin\controller;
/*
 * @Descripttion: 
 * @version: 1.0
 * @Author: Sam
 * @Date: 2022-05-11 09:56:00
 * @LastEditors: Sam
 * @LastEditTime: 2022-12-08 15:22:21
 */
use app\admin\model\User as UserModel;
use app\common\BaseController;
use hg\apidoc\annotation as Apidoc;
use think\Request;

class Welcome extends BaseController{


    /**
     * 首页接口
     * @return void
     * @author Sam
     * @date 2022-05-11
     */
    public function index(){
        $count['user_all'] = UserModel::count();
        $count['user_today'] = UserModel::whereTime('create_time','today')->count();
        $count['user_month'] = UserModel::whereTime('create_time','month')->count();
        $starday = strtotime(date('Y-m-d', strtotime('-30 day')));
        $yesterday = strtotime(date('Y-m-d').' 23:59:59');

        $user_list = UserModel::where('create_time', 'between time', [$starday, $yesterday])
        ->field("FROM_UNIXTIME(create_time,'%m-%e') as day, count(*) as count, FROM_UNIXTIME(create_time,'%Y%m%e') as group_day")
        ->group("group_day")
        ->order('group_day asc')
        ->select()
        ->toArray();
        $chartdata = [];
        $data = [];
        $chartdata['legend'] = ['用户数'];//分类
        $chartdata['yAxis']['maxnum'] = 0;//最大值数量
        $chartdata['xAxis'] = [date('m-d')];//X轴值
        $chartdata['series'] = [0];//分类1值
        if (!empty($user_list)) {
            foreach ($user_list as $k => $v) {
                $data['day'][] = $v['day'];
                $data['count'][] = $v['count'];
                if ($chartdata['yAxis']['maxnum'] < $v['count'])
                    $chartdata['yAxis']['maxnum'] = $v['count'];
            }
            $chartdata['xAxis'] = $data['day'];//X轴值
            $chartdata['series'] = $data['count'];//分类1值
        }
        \json_response(1,'success',compact('count','chartdata'));
    }

}